/**
 * @file: proc.cpp
 * @author: yumeng (imyumeng@qq.com)
 * @brief:
 * @version 0.1
 * @date: 2021-09-06
 *
 * @copyright Meng_Yu Copyright (c) 2021
 *
 * @history: init first version. 2021-09-06
 */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "proc.h"

proc_data_t g_data;

int Read_Tnifile(void)
{
	char tmp_str[STR_LEN] = {0};
	GetPrivateProfileString(INI_SEC_BIN, INI_SEC_SUB_BIN_PATH, INI_DEF_STR, (LPTSTR)g_data.bin_path, STR_LEN, g_data.ini_path_name);
	g_data.bin_num = GetPrivateProfileInt(INI_SEC_BIN, INI_SEC_SUB_BIN_NUM, 0x1, g_data.ini_path_name);
	g_data.bin_num_ofst = GetPrivateProfileInt(INI_SEC_BIN, INI_SEC_SUB_BIN_NUM_OFST, 0x0, g_data.ini_path_name);
	printf("%s,bin_num:%d,num_ofst:%#x\r\n", g_data.bin_path, g_data.bin_num, g_data.bin_num_ofst);
	if (g_data.bin_num > BIN_NUM)
	{
		printf("[E]bin_num max is:%d\r\n", BIN_NUM);
		return -1;
	}
	for (int i = 0; i < g_data.bin_num; i++)
	{
		sprintf(tmp_str, "bin%d", i);
		g_data.addr_ofst[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_ADDR_OFST, 0, g_data.ini_path_name);
		g_data.addr[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_ADDR, 0, g_data.ini_path_name);
		g_data.len_ofst[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_LEN_OFST, 0, g_data.ini_path_name);
		g_data.len[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_LEN, 0, g_data.ini_path_name);
	}
	return 0;
}

int Write_Tnifile(void)
{
	char tmp_str[STR_LEN] = {0};
	char tmp_str1[STR_LEN] = {0};
	WritePrivateProfileString(INI_SEC_BIN, INI_SEC_SUB_BIN_PATH, (LPTSTR)g_data.bin_path, g_data.ini_path_name);
	sprintf(tmp_str, "%d", g_data.bin_num);
	WritePrivateProfileString(INI_SEC_BIN, INI_SEC_SUB_BIN_NUM, tmp_str, g_data.ini_path_name);
	sprintf(tmp_str, "0x%x", g_data.bin_num_ofst);
	WritePrivateProfileString(INI_SEC_BIN, INI_SEC_SUB_BIN_NUM_OFST, tmp_str, g_data.ini_path_name);
	for (int i = 0; i < g_data.bin_num; i++)
	{
		sprintf(tmp_str, "bin%d", i);
		sprintf(tmp_str1, "0x%x", g_data.addr_ofst[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_ADDR_OFST, tmp_str1, g_data.ini_path_name);
		sprintf(tmp_str1, "0x%x", g_data.addr[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_ADDR, tmp_str1, g_data.ini_path_name);
		sprintf(tmp_str1, "0x%x", g_data.len_ofst[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_LEN_OFST, tmp_str1, g_data.ini_path_name);
		sprintf(tmp_str1, "0x%x", g_data.len[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_LEN, tmp_str1, g_data.ini_path_name);
	}
	return 0;
}

void path_proc(char *path)
{
	char *p = path;
	while (*p != 0)
	{
		if('\\' == *p)
		{
			*p = '/';
		}
		p++;
	}
}

